package cn.qdu.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/logout")
public class UserLogoutServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取当前session，如果不存在则不创建新的
        HttpSession session = request.getSession(false);

        if (session != null) {
            // 使当前session失效
            session.invalidate();

            // 可选：添加注销成功的提示信息
            request.getSession().setAttribute("logoutMessage", "您已成功退出登录");
        }

        // 重定向到登录页面，使用绝对路径确保正确跳转
        response.sendRedirect(request.getContextPath() + "/views/auth/login.jsp");
    }

    // 可选：添加POST方法处理
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}